<?php
namespace Common\Service;
use Think\Model;

class CategoryService extends Model{
    public function AddCategory(){
        $model = D('Category');
        try {
            if($model->create()){
                $model->add();
                return array('flag'=>true,'msg'=>'添加成功');
            }else{
                return array('flag'=>false,'msg'=> $model->getError());
            }
        } catch (Exception $e) {
            return array('flag'=>false,'msg'=> $model->getError());
        }

    }

    public function EditCategory(){
        $model = D('Category');
        try {
            if($model->create()){
                $model->save();
                return true;
            }else{
                return $model->getError();
            }
        } catch (Exception $e) {
            $model->getError();
        }
        return false;
    }

    public function DeleteCategory($id){
        $model = M('Category');
        $res = $model->delete($id);
        if($res){
            return true;
        }
        return false;
    }

    public function GetCategoryList(){

    }
    public function GetCategoryByPid($pid,$diaplay=1){
        $model = M('Category');
        $data = null;
        if($diaplay != null){
            $data = $model->where("pid = {$pid} and display = {$diaplay}")->select();
        }else{
            $data = $model->where("pid = {$pid}")->select();
        }
        return $data;
    }

    public function GetCategoryById($id){
        $id = isset($id)?$id : null;
        $model = M('Category');
        $data = $model->find($id);
        return $data;
    }

    public function GetFieldByid($params){
        if(!is_array($params)){
            $params = array();
        }
        $field = isset($params['field'])? $params['field']:'id';
        $id = isset( $params['id'])? $params['id']:'';
        $model = M('Category');
        $res=$model->field($field)->find($id);
        return $res;
    }
}